#coding=utf-8

from aima.search import Node
from aima.utils import (FIFOQueue, Stack)


def custom_graph_search(problem, fringe):
    """Search through the successors of a problem to find a goal.
    The argument fringe should be an empty queue.
    If two paths reach a state, only use the best one. [Fig. 3.18]"""
    
    # Debería devolver el grado de ramificación ¿¿que es??
    # y la cantidad de nodos expandidos.
    
    closed = {}
    fringe.append(Node(problem.initial))
    while fringe:
        node = fringe.pop()
        if problem.goal_test(node.state): 
            return {
                'objetivo': node,
                'cant_nodos' : len(closed),
                'grado_ramificacion' : 'NS/NC',
            }
        if node.state not in closed:
            closed[node.state] = True
            fringe.extend(node.expand(problem))    
    

def custom_breadth_first_graph_search(problem):
    "Search the shallowest nodes in the search tree first. [p 74]"
    return custom_graph_search(problem, FIFOQueue())
    
def custom_depth_first_graph_search(problem):
    "Search the deepest nodes in the search tree first. [p 74]"
    return custom_graph_search(problem, Stack())